package com.jelly.web.modules.basics.dictionary.controller;

import com.jelly.common.constant.Constant;
import com.jelly.common.constant.GlobalConstant;
import com.jelly.common.constant.ShiroConstant;
import com.jelly.common.note.OperationLog;
import com.jelly.common.util.ListUtil;
import com.jelly.common.util.web.AjaxPostResponse;
import com.jelly.common.web.page.LayuiTableInfo;
import com.jelly.config.redis.RedisCacheManager;
import com.jelly.web.share.base.controller.BaseController;
import com.jelly.web.share.base.domain.Base;
import com.jelly.web.modules.basics.dictionary.domain.Dictionary;
import com.jelly.web.modules.basics.dictionary.domain.DiyType;
import com.jelly.web.modules.basics.dictionary.domain.DiyTypeAndDictionary;
import com.jelly.web.modules.basics.dictionary.service.IDictionaryService;
import com.jelly.web.modules.basics.dictionary.service.IDiyTypeAndDictionaryService;
import com.jelly.web.modules.basics.dictionary.service.IDiyTypeService;
import org.apache.shiro.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @创建人 ql
 * @创建时间 2018-09-15 16:49
 * @项目名称 jelly
 * @功能描述: 字典控制器
 **/
@Controller
@RequestMapping("/admin/basics/dictionaries")
public class DictionariesController extends BaseController {

    @Autowired
    private RedisCacheManager redisCacheManager;

    @Autowired
    private IDiyTypeAndDictionaryService diyTypeAndDictionaryService;

    @Autowired
    private IDiyTypeService diyTypeService;

    @Autowired
    private IDictionaryService dictionaryService;
    /**
     * 字典类型列表跳转
     * @return
     */
    @GetMapping
    public String getDiyTypeDictionaryList(){

        //获取字典详情
        getDictionary();
        //获取该用户可访问的按钮详情
        getButton();

        return adminBasicsDictionaries("/list");
    }

    /**
     * 字典类型列表详情
     * @param diyTypeAndDictionary
     * @return
     */
    @PostMapping
    @ResponseBody
    public LayuiTableInfo getDiyTypeDictionaryList(DiyTypeAndDictionary diyTypeAndDictionary){
        return getPageHelper(diyTypeAndDictionaryService.getDiyTypeAndDictionary(diyTypeAndDictionary));
    }

    /**
     * 新增跳转
     * @return
     */
    @GetMapping("/add")
    public String addDiyTypeDictionary(Model model){

        //获取字典详情
        getDictionary();

        model.addAttribute("type",diyTypeService.getDiyType(Constant.SUCCESS));

        return adminBasicsDictionaries("/add");
    }


    /**
     * 新增字典类型字典
     * @param diyType
     * @param dictionary
     * @param showtype
     * @return
     */
    @PostMapping("/add")
    @OperationLog(name = "字典模块",value = GlobalConstant.ADDED)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> addDiyTypeDictionary(DiyType diyType, Dictionary dictionary,String showtype){

        boolean bole = false;

        if("diyType".equals(showtype)){
            bole = diyTypeService.addDiyType(diyType);
        }else {
            bole = dictionaryService.addDictionary(dictionary);
        }
        if(bole){
            updatDiction();
            return okResponse("新增成功");
        }
        return errorResponse("新增失败");

    }

    /**
     * 删除字典类型字典
     * @param base
     * @return
     */
    @PostMapping("/delect")
    @OperationLog(name = "字典模块",value = GlobalConstant.DELECT)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> delectDiyTypeDictionary(Base base){

        boolean bole = false;

        if(!"dictionaries".equals(base.getShowType())){

            if(ListUtil.isNotList(dictionaryService.getDictionaryTypeId(base.getBaseId()))){
                return errorResponse("请删除该类型下的字典后，再删除此类型");
            }
            bole = diyTypeService.delectDiyType(base.getBaseId());
        }else {
            bole = dictionaryService.delectDictionary(base.getBaseId());
        }
        if(bole){
            updatDiction();
            return okResponse("删除成功");
        }
        return errorResponse("删除失败");

    }


    /**
     * 修改菜单权限跳转
     * @param model
     * @param base
     * @return
     */
    @GetMapping("/update")
    public String updateDiyTypeDictionary(Model model, Base base){
        //获取字典详情
        getDictionary();

        if(!"dictionaries".equals(base.getShowType())){
            model.addAttribute("diyTypeDictionary",diyTypeService.getDiyTypeId(base.getBaseId()));
        }else {
            model.addAttribute("diyTypeDictionary",dictionaryService.getDictionaryId(base.getBaseId()));
        }

        model.addAttribute("typeList",diyTypeService.getDiyType(Constant.SUCCESS));

        model.addAttribute("type",base.getShowType());

        return adminBasicsDictionaries("/update");

    }


    /**
     * 修改字典类型字典
     * @param diyType
     * @param dictionary
     * @return
     */
    @PostMapping("/update")
    @OperationLog(name = "字典模块",value = GlobalConstant.UPDATE)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> updateDiyTypeDictionary(DiyType diyType, Dictionary dictionary){

        boolean bole = false;

        if(!"dictionaries".equals(diyType.getShowType())){
            bole = diyTypeService.updateDiyType(diyType);
        }else {
            bole = dictionaryService.updateDictionary(dictionary);
        }
        if(bole){
            updatDiction();
            return okResponse("修改成功");
        }
        return errorResponse("修改失败");

    }

    /**
     * 删除缓存信息
     */
    public void updatDiction(){
        Cache<String, Object> cache = redisCacheManager.getCache(ShiroConstant.SHIROREDISCACHE);
        cache.remove(SHIRODICTIONY);
    }

}
